package org.amphiprion.descentv2assistant.entity;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.amphiprion.descentv2assistant.dao.xml.ConditionXmlDao;
import org.amphiprion.descentv2assistant.dao.xml.LieutenantXmlDao;
import org.amphiprion.descentv2assistant.dao.xml.MonsterXmlDao;

import android.database.Cursor;

public class EncounterMonster extends Entity implements Comparable<EncounterMonster> {
	private IGenericMonster monster;
	private final int act;
	private String tag;
	private int health;
	private String gameId;
	private final List<Condition> conditions = new ArrayList<Condition>();

	public EncounterMonster(int act) {
		super(UUID.randomUUID().toString());
		this.act = act;
	}

	public EncounterMonster(Cursor cursor) {
		id = cursor.getString(cursor.getColumnIndex("id"));
		tag = cursor.getString(cursor.getColumnIndex("tag"));
		health = cursor.getInt(cursor.getColumnIndex("health"));
		act = cursor.getInt(cursor.getColumnIndex("act"));
		gameId = cursor.getString(cursor.getColumnIndex("game_session_id"));

		String strCondition = cursor.getString(cursor.getColumnIndex("condition"));
		if (strCondition != null && !"".equals(strCondition)) {
			String[] strs = strCondition.split(";");
			for (int i = 0; i < strs.length; i++) {
				int pos = strs[i].indexOf("|");
				Condition c = ConditionXmlDao.getInstance().getCondition(strs[i].substring(0, pos)).clone();
				c.setCount(Integer.parseInt(strs[i].substring(pos + 1)));
				conditions.add(c);
			}
		}

		String monsterId = cursor.getString(cursor.getColumnIndex("mob_id"));
		String monsterType = cursor.getString(cursor.getColumnIndex("mob_type"));
		if (monsterType.equals("Monster")) {
			monster = MonsterXmlDao.getInstance().getMonster(monsterId, act);
		} else if (monsterType.equals("Lieutenant")) {
			monster = LieutenantXmlDao.getInstance().getLieutenant(monsterId, act);
		}
		if (health == -666) {
			health = monster.getCharacteristic().getHealth();
		}
	}

	public int getAct() {
		return act;
	}

	public IGenericMonster getMonster() {
		return monster;
	}

	public int getHealth() {
		return health;
	}

	public void setHealth(int health) {
		this.health = health;
	}

	public String getTag() {
		return tag;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}

	public List<Condition> getConditions() {
		return conditions;
	}

	public void setMonster(IGenericMonster monster) {
		this.monster = monster;
	}

	public String getGameId() {
		return gameId;
	}

	public void setGameId(String gameId) {
		this.gameId = gameId;
	}

	@Override
	public int compareTo(EncounterMonster another) {
		return monster.getName().compareTo(another.getMonster().getName());
	}

	public String getConditionsForDB() {
		if (conditions.size() == 0) {
			return null;
		}
		StringBuffer sb = new StringBuffer();
		for (Condition c : conditions) {
			if (sb.length() > 0) {
				sb.append(";");
			}
			sb.append(c.getId());
			sb.append("|");
			sb.append(c.getCount());
		}
		return sb.toString();
	}

}
